/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <QSettings>
#include <QMetaEnum>
#include <QDebug>
#include <QTextCodec>
#ifdef USE_KYSDK_SYSINFO
#include "kysdk/kysdk-system/libkysysinfo.h"
#endif
#define DEFAULT_CONTAINER_ROW 3
#define DEFAULT_CONTAINER_COLUMN 4

Config::Config(QObject *parent)
    : QObject{parent}
{
    m_confSettings = new QSettings(kConfFile, QSettings::IniFormat, this);
    defaultContainers();
}

Config::DesktopMode Config::desktopMode() const
{
#ifdef USE_KYSDK_SYSINFO
    char* systemCategory = kdk_system_get_systemCategory();
    QString mode = systemCategory;
    free(systemCategory);
    qDebug() << __func__ << mode;
    if (mode == nullptr || mode == "none") {
        qWarning() << __func__ << "get desktop mode failed !!!";
        return DesktopMode::Tablet;
    }
    if (mode == "Tablet") {
        return DesktopMode::Tablet;
    }
    if (mode == "MaxTablet") {
        return DesktopMode::EduScreen;
    }
#endif
    return DesktopMode::Tablet;
}

QStringList Config::appWidgetList() const
{
    return m_confSettings->value("Desktop/appWidget").toStringList();
}

std::tuple<int, int> Config::desktopLayout() const
{
    QMetaEnum meta = QMetaEnum::fromType<DesktopMode>();
    QString mode = meta.valueToKey(desktopMode());
    QString layout = "Layout/" + mode;
    QString strLayout = m_confSettings->value(layout).toString();
    QStringList strListLayout = strLayout.split("x", QString::KeepEmptyParts, Qt::CaseInsensitive);
    if (strListLayout.size() != 2 ||
            strListLayout.at(0).toInt() <= 0 ||
            strListLayout.at(1).toInt() <= 0) {
        qWarning() << "Config file layout error " << strLayout;
        return std::make_tuple(4, 6);
    }
    return std::make_tuple(strListLayout.at(0).toInt(), strListLayout.at(1).toInt());
}

QMap<QString, QStringList> Config::defaultGroups() const
{
    QMap<QString, QStringList> groups;
    m_confSettings->beginGroup("DefaultGroup");
    QStringList groupNames = m_confSettings->allKeys();
    for (auto &name : qAsConst(groupNames)) {
        groups[name] = m_confSettings->value(name).toStringList();
    }
    m_confSettings->endGroup();
    return groups;
}

QList<Config::ContainerInfo> Config::defaultContainers() const
{
    QList<Config::ContainerInfo> containers;
    m_confSettings->beginGroup("DefaultContainer");
    QStringList containerNames = m_confSettings->value("containers").toStringList();
    m_confSettings->endGroup();
    for (auto &container : qAsConst(containerNames)) {
        Config::ContainerInfo containerInfo;
        m_confSettings->beginGroup(container);
        containerInfo.containerName = container.toLatin1();
        containerInfo.desktopFiles = m_confSettings->value("apps").toStringList();
        containerInfo.placeholder = layoutList(m_confSettings->value("layout").toString());
        containerInfo.titleEditable = m_confSettings->value("titleEditable").toBool();
        containerInfo.containerEditable = m_confSettings->value("containerEditable").toBool();
        containerInfo.containerCategory = m_confSettings->value("category").toString();
        containers.append(containerInfo);
        m_confSettings->endGroup();
    }
    return containers;
}

QList<int> Config::layoutList(QString layout) const
{
    QList<int> layoutList;
    QString strLayout = layout;
    QStringList strListLayout = strLayout.split("x", QString::KeepEmptyParts, Qt::CaseInsensitive);
    if (strListLayout.size() != 2 ||
            strListLayout.at(0).toInt() <= 0 ||
            strListLayout.at(1).toInt() <= 0) {
        qWarning() << __func__ << "Config file layout error " << strLayout;
        layoutList.push_back(DEFAULT_CONTAINER_ROW);
        layoutList.push_back(DEFAULT_CONTAINER_COLUMN);
        return layoutList;
    }
    layoutList.push_back(strListLayout.at(0).toInt());
    layoutList.push_back(strListLayout.at(1).toInt());
    return layoutList;
}
